package com.innovations.inn.controller;

import com.innovations.common.annotation.Log;
import com.innovations.common.core.controller.BaseController;
import com.innovations.common.core.domain.AjaxResult;
import com.innovations.common.core.page.TableDataInfo;
import com.innovations.common.enums.BusinessType;
import com.innovations.common.utils.poi.ExcelUtil;
import com.innovations.framework.web.service.TokenService;
import com.innovations.inn.domain.ResearchOutcomesReviews;
import com.innovations.inn.service.IResearchOutcomesReviewsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 科研成果评价Controller
 *
 * @author dp
 * @date 2024-08-09
 */
@Api(tags = "科研成果评价")
@RestController
@RequestMapping("/inn/outcomes/reviews")
public class ResearchOutcomesReviewsController extends BaseController {
    @Autowired
    private IResearchOutcomesReviewsService researchOutcomesReviewsService;
    @Autowired
    TokenService tokenService;

    /**
     * 查询科研成果评价列表
     */
    @ApiOperation("查询科研成果评价列表")
    @PreAuthorize("@ss.hasPermi('inn:reviews:list')")
    @GetMapping("/list")
    public TableDataInfo list(ResearchOutcomesReviews researchOutcomesReviews) {
        startPage();
        List<ResearchOutcomesReviews> list = researchOutcomesReviewsService.selectResearchOutcomesReviewsList(researchOutcomesReviews);
        return getDataTable(list);
    }

    /**
     * 导出科研成果评价列表
     */
    @PreAuthorize("@ss.hasPermi('inn:reviews:export')")
    @Log(title = "科研成果评价", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ResearchOutcomesReviews researchOutcomesReviews) {
        List<ResearchOutcomesReviews> list = researchOutcomesReviewsService.selectResearchOutcomesReviewsList(researchOutcomesReviews);
        ExcelUtil<ResearchOutcomesReviews> util = new ExcelUtil<ResearchOutcomesReviews>(ResearchOutcomesReviews.class);
        util.exportExcel(response, list, "科研成果评价数据");
    }

    /**
     * 获取科研成果评价详细信息
     */
    @ApiOperation("获取科研成果评价详细信息")
    @PreAuthorize("@ss.hasPermi('inn:reviews:query')")
    @GetMapping(value = "/{reviewsId}")
    public AjaxResult getInfo(@PathVariable("reviewsId") Long reviewsId) {
        return success(researchOutcomesReviewsService.selectResearchOutcomesReviewsByReviewsId(reviewsId));
    }

    /**
     * 新增科研成果评价
     */
    @ApiOperation("新增科研成果评价")
    @PreAuthorize("@ss.hasPermi('inn:reviews:add')")
    @Log(title = "科研成果评价", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(HttpServletRequest request, @RequestBody ResearchOutcomesReviews researchOutcomesReviews) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        researchOutcomesReviewsService.completeData(researchOutcomesReviews, userId, 1);
        return toAjax(researchOutcomesReviewsService.insertResearchOutcomesReviews(researchOutcomesReviews));
    }

    /**
     * 修改科研成果评价
     */
    @ApiOperation("修改科研成果评价")
    @PreAuthorize("@ss.hasPermi('inn:reviews:edit')")
    @Log(title = "科研成果评价", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(HttpServletRequest request, @RequestBody ResearchOutcomesReviews researchOutcomesReviews) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        researchOutcomesReviewsService.completeData(researchOutcomesReviews, userId, 2);
        return toAjax(researchOutcomesReviewsService.updateResearchOutcomesReviews(researchOutcomesReviews));
    }

    /**
     * 删除科研成果评价
     */
    @ApiOperation("删除科研成果评价")
    @PreAuthorize("@ss.hasPermi('inn:reviews:remove')")
    @Log(title = "科研成果评价", businessType = BusinessType.DELETE)
    @DeleteMapping("/{reviewsIds}")
    public AjaxResult remove(@PathVariable Long[] reviewsIds) {
        return toAjax(researchOutcomesReviewsService.deleteResearchOutcomesReviewsByReviewsIds(reviewsIds));
    }
}
